/*
Table 4: Dummy for Assistance Received (by type of assistance and provider of the assistance -- i.e., individuals, government, or NGOs)
Table A4 Counterpart of Table 4 with HTE (heterogeneous effects by baseline household asset wealth)
*/

est clear
version 14.0
set more off


// DATA
use "household_level_final.dta", clear
iis hid_base

// Construct deflated transfer amount vars for imbalanced baseline values

foreach x in trans_from_tot 	    trans_from_ind {
loc labl: var label `x'
	replace `x'=`x'/1000
	label var `x' "`labl' (1000s TSH)"
	
	clonevar `x'_09cons=`x'
	replace `x'_09cons=`x'_09cons/1.196777893 if period==2
	replace `x'_09cons=`x'_09cons/1.388277649 if period==3
	label var `x'_09cons "`labl' in 1000s cons. 2009 TSH"
}

// OUTCOMES AND CONTROL VECTORS (VARIABLES IMBALANCED AT BASELINE)
loc outcomes = "dm_cash_from_ind 	dm_food_from_ind 		dm_inkind_from_ind 		dm_cash_from_gvt 	dm_food_from_gvt	dm_inkind_from_gvt	dm_cash_from_ngo 	dm_food_from_ngo 		dm_inkind_from_ngo	"
loc outcomes1 = "dm_food_from_ind 		dm_inkind_from_ind 		dm_cash_from_gvt 	dm_food_from_gvt	dm_inkind_from_gvt	dm_cash_from_ngo 	dm_food_from_ngo 		dm_inkind_from_ngo	"
loc imbalancedvars = "improvedfloor_b	dm_trust_comm	dm_food_from_ind	trans_from_tot_09cons	trans_from_ind_09cons	"


// REGRESSIONS

//without HTE, controlling for variables imbalanced at baseline interacted with a time trend

foreach y in `imbalancedvars' {
	clonevar temp= `y'
	replace temp=. if period!=1
	bysort hid_base: egen `y'_b=max(temp)
	loc labb: var label `y'
	gen im_`y'_b_pd2=`y'_b*dmpd2
	label var im_`y'_b_pd2 "BL val of `labb' x pd 2"
	gen im_`y'_b_pd3=`y'_b*dmpd3
	label var im_`y'_b_pd3 "BL val of `labb' x pd 3"
	drop temp 
}

qui foreach y in `outcomes' {
	eststo: xtreg 	`y'		assignedtotreat2 	assignedtotreat3	dmpd2	dmpd3 	im_*,  cluster(CLID)	fe
}
noi estout *, cells(	b(fmt(%9.0f)star)	se(fmt(%9.0f)) 	) 	stats(N r2,fmt(0 3)) starlevels(* 0.10 ** 0.05 *** 0.01) 

esttab using Table4.csv, b(3) se(3) r2 starlevels(* 0.1 ** .05 *** .01) obslast replace



//Heterogeneous treatment effects by baseline asset wealth poverty

est clear
foreach k in   		  assignedtotreat2 	assignedtotreat3 dmpd2 dmpd3	im_improvedfloor_b_b_pd2 im_improvedfloor_b_b_pd3 im_dm_trust_comm_b_pd2 im_dm_trust_comm_b_pd3 im_dm_food_from_ind_b_pd2 im_dm_food_from_ind_b_pd3 im_trans_from_tot_09cons_b_pd2 im_trans_from_tot_09cons_b_pd3 im_trans_from_ind_09cons_b_pd2 im_trans_from_ind_09cons_b_pd3 {
	loc labl: var label `k'
	gen	I`k'		=	dm_poor*`k'	//"I" is for "interact"
	lab var I`k' "`labl' X dummy - poor"
}

loc labb: var label dm_cash_from_ind
xtreg dm_cash_from_ind assignedtotreat2 assignedtotreat3 dmpd2 dmpd3 Iassignedtotreat2 Iassignedtotreat3 Idmpd2 Idmpd3 im_* Iim_* , cluster(CLID) fe
forv p = 2/3 {
		**get p-value for interaction term
		loc tt = _b[Iassignedtotreat`p']/_se[Iassignedtotreat`p']
		loc pvaldiff`p'		=  2*ttail(		e(df_r),		abs(`tt')	)	
	
		lincom 	assignedtotreat`p' 	+		Iassignedtotreat`p'		
		loc bI`p'		= 	r(estimate)
		loc seI`p'		=	r(se)
		test 	assignedtotreat`p' +	Iassignedtotreat`p' 	==0
		loc pI`p'		=	r(p)
	}
outreg2 using TableA4.xls, bdec(3) pdec(11)  stats(coef se) label replace ctitle("HTE by baseline poverty", "`labb'")	///
		addstat(linear combination of parameters coeff (midline),	`bI2',  std. error(midline), `seI2', ///
		p-value (midline), `pI2',p-value of difference (midline) , `pvaldiff2', ///
		linear combination of parameters coeff (endline),	`bI3',  ///
		std. error(endline), `seI3',  	p-value (endline), `pI3', p-value of difference (endline) , `pvaldiff3')	
est clear
foreach y in `outcomes1'	{
	loc labb: var label `y'
	xtreg 	`y'		 assignedtotreat2 	 assignedtotreat3	 dmpd2	 dmpd3	///
					Iassignedtotreat2 	Iassignedtotreat3	Idmpd2	Idmpd3 im_* Iim_* ,	cluster(CLID)	fe


	forv p = 2/3 {
		**get p-value for interaction term
		loc tt = _b[Iassignedtotreat`p']/_se[Iassignedtotreat`p']
		loc pvaldiff`p'		=  2*ttail(		e(df_r),		abs(`tt')	)	
	
		lincom 	assignedtotreat`p' 	+		Iassignedtotreat`p'		
		loc bI`p'		= 	r(estimate)
		loc seI`p'		=	r(se)
		test 	assignedtotreat`p' +	Iassignedtotreat`p' 	==0
		loc pI`p'		=	r(p)
	}
	
	outreg2 using TableA4.xls, bdec(3) pdec(11)  stats(coef se) label append ctitle("HTE by baseline poverty", "`labb'")	///
		addstat(linear combination of parameters coeff (midline),	`bI2',  std. error(midline), `seI2', ///
		p-value (midline), `pI2', p-value of difference (midline) , `pvaldiff2', ///
	    linear combination of parameters coeff (endline),	`bI3',  ///
		std. error(endline), `seI3',  	p-value (endline), `pI3',p-value of difference (endline) , `pvaldiff3')		

}

est clear


exit
